<template>
  <div class="home">
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>关键词：</label>
            <el-input type="text" placeholder="请输入关键词" v-model="keyword" clearable></el-input>
          </div>
          <div class="operate_item">
            <label>商品类型：</label>
            <el-select
              filterable
              v-model="typeId"
              placeholder="请选择商品类型">
              <el-option label="全部" :value="''"></el-option>
              <el-option
                v-for="(item,index) in typeOption"
                :key="index"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span class="operate_btn search_btn" @click="openPopup('')">新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            width="80"
            prop="id"
            label="ID">
          </el-table-column>
          <el-table-column
            width="160"
            sortable
            prop="modifyTime"
            label="修改时间">
            <template slot-scope="scope">
              <p style="text-align: left">{{ formatTime(scope.row.modifyTime) }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="商品名称">
          </el-table-column>
          <el-table-column
            width="120"
            label="logo">
            <template slot-scope="scope">
              <img :src="scope.row.photoUrl" alt="tzs" class="icon" v-if="scope.row.photoUrl"
                   @click="openImg(scope.row.photoUrl)">
              <p v-else>——</p>
            </template>
          </el-table-column>
          <el-table-column
            width="140"
            prop="productTypeName"
            label="商品类型">
          </el-table-column>
          <el-table-column
            sortable
            width="120"
            prop="price"
            label="销售价(元)">
            <template slot-scope="scope">{{ scope.row.price ? scope.row.price : '——' }}</template>
          </el-table-column>

          <!-- 客服、配送员不可见-->
          <el-table-column
            v-if="(userType!==7) && (userType!==6)"
            sortable
            width="120"
            prop="buyPrice"
            label="进货价(元)">
            <template slot-scope="scope">{{ scope.row.buyPrice ? scope.row.buyPrice : '——' }}</template>
          </el-table-column>
          <el-table-column
            width="120"
            label="是否回收空桶">
            <template slot-scope="scope">
              <div :style="'color:'+(scope.row.isReturn===1?'#67c430':'#e60000')">
                {{ scope.row.isReturn === 1 ? '是' : '否' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            width="120"
            label="是否需要押金">
            <template slot-scope="scope">
              <div :style="'color:'+(scope.row.isDeposit===1?'#67c430':'#e60000')">
                {{ scope.row.isDeposit === 1 ? '是' : '否' }}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            width="120"
            label="状态" v-if="!(btn.freeze||btn.unfreeze)">
            <template slot-scope="scope">
              <div :style="'color:'+(scope.row.enable===1?'#67c430':'#e60000')">
                {{ scope.row.enable === 1 ? '可用' : '禁用' }}
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="状态"
            prop="2"
            sortable="custom"
            width="100" v-if="btn.freeze||btn.unfreeze">
            <template slot-scope="scope">
              <div>
                <el-switch
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  @change="deleteItem(scope.row,scope.$index)"
                  v-model="scope.row.enable">
                </el-switch>
                <span class="normal" v-if="scope.row.enable===1">启用</span>
                <span class="unusual" v-else>禁用</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            fixed="right"
            v-if="btn&&(btn.update || btn.queryPhoto)"
            width="150">
            <template slot-scope="scope">
              <div class="table_btn">
                <span class="change_info" @click="openPopup(scope.row)" v-if="btn.update">编辑</span>
                <span class="change_info" @click="openPopup(scope.row,1)" v-if="btn.queryPhoto">详情图</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{ totals }}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
    <edit v-if="isPop" :productId="productId" @listenToChild="listenToEdit"></edit>
    <showImg :icon="icon" @listenToChildEvent="getOpen" v-if="isOpen"></showImg>
    <el-dialog :title="productName + '商品详情图'" :visible.sync="isImg" width="80%" @close="isImg=false;" :append-to-body="true">
      <detailImg :productId="productId" v-if="isImg"></detailImg>
    </el-dialog>
  </div>
</template>

<script>

import edit from "./edit";
import showImg from "../../../components/ShowImg";
import commons from "@/utils/common";
import {listData, updateEnable} from "@/api/config/product";
import {getAllType} from "../../../api/config/productType";
import {getBtn} from "@/utils/btnAuth";
import detailImg from './detailImgs'

export default {
  components: {
    edit,
    showImg,
    detailImg
  },
  data() {
    return {
      keyword: '',
      typeId: '',
      typeOption: [],
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      tableInfo: [],
      isPop: false,
      productId: '',
      btn: {},
      url: '',
      isOpen: false,
      isImg: false,
      productName:'',
      userInfo:{},
      userType:''
    }
  },
  mounted() {
    this.getTypeOption();
    //获取用户类型
    let info = localStorage.getItem('water_user')
    this.userInfo = info?JSON.parse(info).loginUser:'';
    this.userType = this.userInfo.type;
    this.btn = getBtn();
    if (this.btn.query) {
      this.getTableInfo(1);
    }
  },
  methods: {
    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit: that.pageSize,
        page: page,
        name: that.keyword,
        productTypeId: that.typeId
      }
      listData(queryParams).then(res => {
        that.tableInfo = res ? res.list : [];
        that.totals = res ? res.count : 0;
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    // 重置
    resetTable() {
      this.pageSize = 15;
      this.keyword = '';
      this.typeId = '';
      this.getTableInfo(1)
    },

    getTypeOption() {
      getAllType().then(res => {
        this.typeOption = res ? res : [];
      }).catch((e) => {
        this.typeOption = [];
      });
    },

    // 删除
    deleteItem(obj, index) {
      let that = this;
      that.$confirm('确定' + (obj.enable === 0 ? '禁用' : '启用') + obj.name + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {id: obj.id, enable: obj.enable}
        return updateEnable(param);
      }).then(() => {
        that.getTableInfo(that.currentPage);
        that.$message.success({
          message: '修改成功！',
          duration: 1500
        });
      }).catch(() => {
        //重置
        that.tableInfo[index].enable = obj.enable == 0 ? 1 : 0;
      });
    },
    // 打开弹窗
    openPopup(obj, type) {
      this.productId = obj.id;
      if (type) {
        this.productName = obj.name;
        this.isImg = true;
      } else {
        this.isPop = true;
      }
    },

    listenToEdit(e) {
      this.isPop = false;
      if (e.refresh) {
        this.getTableInfo(this.currentPage)
      }
    },

    formatTime(time) {
      return commons.formatTime(time)
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },
    //显示大图
    openImg(url) {
      this.isOpen = true;
      this.icon = url;
    },
    getOpen(e) {
      this.isOpen = e;
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style scoped>

</style>
